<?php
/*
Plugin Name: WordPress WebCam Image Viewer
Plugin URI: http://www.bloxblog.com
Description: Module to display webcam images from a constantly refreshing image on a remote server
Author: William Mincy + Ken Jameson
Version: 1.0.2
Author URI: http://www.bloxblog.com
*/



function wp_imcam_addoptionpage() {
	if (function_exists('add_options_page')) {
		add_options_page('WordPress WebCam Image Viewer Options', 'WebCam Image', 0, basename(__FILE__), 'wp_imgcam_optionspage');
    }
 }

function wp_imgcam_optionspage() {
	$options = get_option('webcamimgoptions');
	$del = ':-:';
	if($options==NULL) {
		$empty=false;
	} else {
		$vals=explode($del,$options);
	}
	?>
	
	<?php
	if($_POST) {
		$newval = $_POST['u'][0].$del.(int)$_POST['u'][1].$del.(int)$_POST['u'][2].$del.(int)$_POST['u'][3].$del.urlencode($_POST['u'][4]).$del.(int)$_POST['u'][5].$del.urlencode($_POST['u'][6]).$del.urlencode($_POST['u'][7]).$del.(int)$_POST['u'][8];
		update_option('webcamimgoptions',$newval,'Options for WebCam image Viewer','no');
		$vals=explode($del,$newval);
		?>
		<div class="updated"><p><strong>
			<?php _e('Updates have been made successfully.'); ?>
		</strong></p></div>
	<?php 
	} 
	?>

	<?php if($options==NULL) { ?>
	<div class="error"><p><?php _e('No Options Currently Set.'); ?></p></div>
	<?php } ?>
	
	<div class="wrap">
	<h2>WordPress WebCam Image Viewer</h2>
			
			<style type="text/css" media="screen">
				#wpimcamform {
					width:80%;
					margin:0 auto;
				}
				
				#wpimcamform label {
					width:300px;
					display:block;
					clear:left;
					margin-top:5px;
					margin-right:5px;
				}
				
				#wpimgcam_instr {
					width:80%;
					margin:5px auto;
				}
				
				.redt {
					color:red;
					font-weight:bold;
				}		
			</style>
			<?php if($options!=NULL) { ?>
			<div style="clear:both; float:right; clear:left; margin-bottom:10px; width:400px; text-align:right; padding-right:10px;">[ <a href="#previewcam">Preview WebCam</a> ]</div>
			<?php } ?>
			<form action="?page=wp_imgcam.php" method="post" id="wpimcamform">
				<span class="redt">Required fields in red</span>
				<label class="redt">Webcam URL: </label><input type="text" name="u[0]" value="<?=$vals[0]?>" size="80" /><br />
				<label>Webcam Title: </label><input type="text" name="u[4]" value="<?php echo stripslashes(urldecode($vals[4])); ?>" size="50" /><br />
				<label>Width of image: </label><input type="text" name="u[1]" value="<?=$vals[1]?>" size="15" /><br />
				<label>Height of image: </label><input type="text" name="u[2]" value="<?=$vals[2]?>" size="15" /><br />
				<label class="redt">Refresh rate (seconds): </label><input type="text" name="u[3]" value="<?=$vals[3]?>" size="5" /><br />
				<p>
				<strong>Optional Settings</strong>
				<label>Open image in pop-up chromeless window: <input type="checkbox" name="u[5]" value="1"
					<?php if($vals[5]==1) { ?> checked="checked" <?php } ?> /> </label>
				<label>Show external website link below image: <input type="checkbox" name="u[8]" value="1"
					<?php if($vals[8]==1) { ?> checked="checked" <?php } ?> /> </label>
				<label>Link to external website:</label><input type="text" name="u[6]" value="<?php echo stripslashes(urldecode($vals[6])); ?>" size="40" /><br />
				<label>Text for external link:</label><input type="text" name="u[7]" value="<?php echo stripslashes(urldecode($vals[7])); ?>" size="40" /><br />
				</p>
				<div class="submit">
					<input type="submit" name="info_update" value="<?php _e('Update options', 'Localization name'); ?> &raquo;" />
				</div>
			</form>
			<div id="wpimgcam_instr">
			<h5>Usage Instructions</h5>
			This plugin allows your website to show a constantly resfreshing image from a webcam URL. There are two ways to call this into your site:
				<ol>
					<li><strong>Place the [[wpcam]] article into your content</strong><br/>By placing the string [[wpcam]] into your content for a page or post, this will trigger the internal content filter and replace that text with the appropriate HTML to show the webcam image</li>
					<li><strong>Call the function directly from your template</strong><br/>Calling the rendering function directly from your template will place this onto the page in the position you need your webcam image to live in. The code to execute this from within the template is:
					<code style="clear:both; margin:5px auto; display:block; text-align:center; width:100%;">
						if(function_exists('show_wpimgcam_viewer')) {
							show_wpimgcam_viewer();
						}
					</code>
					This will render out the appropriate HTML to show the webcam viewer.
					</li>
				</ol>
			</div>
	<?php if($options!=NULL) {
		show_wpimgcam_viewer();
	} ?>
	</div>
	<?php
}

function show_wpimgcam_viewer($method=0) {
	$options = get_option('webcamimgoptions');
	$vals = explode(':-:',$options);
		
		$vals[6]=stripslashes(urldecode($vals[6]));
		
		if(substr($vals[6],0,4)!='http') {
			$vals[6]='http://'.$vals[6];
		}
		
		if($vals[1]<2 || $vals[2]<2) {
			$theimginfo = getimagesize($vals[0]);
			$vals[1]=$theimginfo[0];
			$vals[2]=$theimginfo[1];
		}
		
		if(strlen($vals[6])>7) {
			$bottomlink .= '<span style="display:block; margin-bottom:4px;"><a href="'.$vals[6].'" target="_blank">'.urldecode($vals[7]).'</a></span>';
		}
		
	$output = '
		<script language="JavaScript" type="text/javascript">
			var imgsrc = "'.$vals[0].'";
			var curr = "currentCamImg1";
			var odiv = "currentCamImg2";
			function changecamimg() {
				var tmp = new Date();
				var c = document.getElementById("camimg");
				c.src = imgsrc+"?"+tmp.getTime();
			}
			
			setInterval(\'changecamimg()\','.$vals[3].'000);
			';
		if($vals[5]==1) {
			$output .= '
			 //really not important (the first two should be small for Opera\'s sake)
			PositionX = 10;
			PositionY = 10;
			defaultWidth  = 600;
			defaultHeight = 400;
			
			//kinda important
			var AutoClose = true;
			
			//don\'t touch
			function popImage(imageURL,imageTitle){
				var imgWin = window.open(\'\',\'_blank\',\'scrollbars=no,toolbar=no,status=no,menubar=no,resizable=1,width=\'+defaultWidth+\',height=\'+defaultHeight+\',left=\'+PositionX+\',top=\'+PositionY);
				if( !imgWin ) { return true; } //popup blockers should not cause errors
				
				imgWin.document.write(\'<html><head><title>\'+imageTitle+\'<\/title><script type="text\/javascript">\'+
					\'function resizeWinTo() {\'+
					\'if( !document.images.length ) { document.images[0] = document.layers[0].images[0]; }\'+
					\'var oH = document.images[0].height, oW = document.images[0].width;\'+
					\'if( !oH || window.doneAlready ) { return; }\'+ //in case images are disabled
					\'window.doneAlready = true;\'+ //for Safari and Opera
					\'var x = window; x.resizeTo( oW + 200, oH + 200 );\'+
					\'var myW = 0, myH = 0, d = x.document.documentElement, b = x.document.body;\'+
					\'if( x.innerWidth ) { myW = x.innerWidth; myH = x.innerHeight; }\'+
					\'else if( d && d.clientWidth ) { myW = d.clientWidth; myH = d.clientHeight; }\'+
					\'else if( b && b.clientWidth ) { myW = b.clientWidth; myH = b.clientHeight; }\'+
					\'if( window.opera && !document.childNodes ) { myW += 16; }\'+
					\'x.resizeTo( oW = oW + ( ( oW + 200 ) - myW ), oH = oH + ( (oH + 200 ) - myH ) );\'+
					\'var scW = screen.availWidth ? screen.availWidth : screen.width;\'+
					\'var scH = screen.availHeight ? screen.availHeight : screen.height;\'+
					\'if( !window.opera ) { x.moveTo(Math.round((scW-oW)/2),Math.round((scH-oH)/2)); }\'+
					\'}\'+
					\'var imgsrc = "'.$vals[0].'";\'+
					\'var curr = "currentCamImg1";\'+
					\'var odiv = "currentCamImg2";\'+
					\'function changecamimg() {\'+
					\'	var tmp = new Date();\'+
					\'	var c = document.getElementById("camimg");\'+
					\'	c.src = imgsrc+"?"+tmp.getTime();\'+
					\'}\'+
					\'setInterval(\\\'changecamimg()\\\','.$vals[3].'000);\'+
					\'<\/script>\'+';
			$output .= '\'<\/head><body marginheight="0" marginwidth="0" onload="resizeWinTo();"\'+\'>\'+';
			$output .= '
					(document.layers?(\'<layer left="0" top="0">\'):(\'<div style="position:absolute;left:0px;top:0px;">\'))+
					\'<a href="#" onClick="javascript:window.close();"><div id="wp_ajaxcam"><img id="camimg" border="0" src=\'+imageURL+\' alt="Click to close image" title="Click to close image" onClick="javascript:window.close();" onload="resizeWinTo();"></a></div>\'+
					(document.layers?\'<\/layer>\':\'<\/div>\')+\'<\/body><\/html>\');
				imgWin.document.close();
				if( imgWin.focus ) { imgWin.focus(); }
				return false;
			}';
		}
		
		
		
		$output .= '
		</script>
		<style type="text/css" media="screen">
			#wp_ajaxcam {
				margin:0 auto;
				width:100%;
				text-align:center;
				clear:both;
				height:';
		$output .= (isset($bottomlink)) ? $vals[2]+25 : $vals[2]; 		
		$output .= 'px;
			}
		</style>
		<a name="previewcam"></a><h4>Webcam Preview</h4>
		<div id="wp_ajaxcam">
		';
		
		$theimage =	'	<img id="camimg" src="'.$vals[0].'"';
		if(strlen($vals[4])>1) { $theimage .= ' alt="'.stripslashes(urldecode($vals[4])).'"'; }
		if($vals[1]>0) { $theimage .= ' width="'.$vals[1].'"'; }
		if($vals[2]>0) { $theimage .= ' height="'.$vals[2].'"'; }
		$theimage .= ' />';
		
		if($vals[5]==1) {
			$output .= '<a href="'.$vals[0].'"  onClick="return popImage(this.href,\''.addslashes(urldecode($vals[4])).'\')">'.$theimage.'</a>';
		} else {
			$output .= $theimage;
		}
		
		if(isset($bottomlink) && $vals[8]==1) { 
			$output .= $bottomlink;
		}
		
		$output .= '
		</div>';
	
	if($method==0) {
		echo $output;
	} else {	
		return $output;
	}
}

function filter_wpcam_viewer(&$content) {
	$temp = explode('[[wpcam]]',$content);
	if(sizeof($temp)>1) { 
		$content = str_replace('[[wpcam]]',show_wpimgcam_viewer(1),$content);
	}
	return $content;
}

add_action('admin_menu', 'wp_imcam_addoptionpage');
add_action('the_content', 'filter_wpcam_viewer',5);

?>